package com.ddstation.app.holiday.controller;

import com.ddstation.app.holiday.model.WeeklyParamModel;
import com.ddstation.app.holiday.service.HolidayService;
import com.ddstation.crud.entity.DtInvsInstitutionHolidayEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AppHolidayController {

    @Inject
    HolidayService holidayService;

    @RequestMapping(value="appinvs_holiday.do/load", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getInstitutionHoliday(@RequestBody Map<String, Object> params) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("errCode", -1);
        String institutionId = (String)params.get("institutionId");
        int year = Integer.parseInt(params.get("year").toString());
        int month = Integer.parseInt(params.get("month").toString());
        LocalDate startDate = LocalDate.of(year, month, 1);
        LocalDate endDate = startDate.plusMonths(1L).plusDays(6L);
        startDate = startDate.minusDays(7L);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<DayOfWeek> weekdays = holidayService.getResWeekdays(institutionId);
        Map<String, DtInvsInstitutionHolidayEntity> holidayMap = new HashMap<>();
        LocalDate iDate = startDate;
        while (!iDate.isAfter(endDate)) {
            if (weekdays.contains(iDate.getDayOfWeek())) {
                DtInvsInstitutionHolidayEntity entity = new DtInvsInstitutionHolidayEntity();
                entity.setIsHoliday(1);
                String date = dtf.format(iDate);
                entity.setDate(date);
                holidayMap.put(date, entity);
            }
            iDate = iDate.plusDays(1);
        }
        List<DtInvsInstitutionHolidayEntity> externalHolidays = holidayService.getHolidaysByDuration(dtf.format(startDate), dtf.format(endDate), institutionId);
        for (DtInvsInstitutionHolidayEntity entity:externalHolidays) {
            holidayMap.put(entity.getDate(), entity);
        }
        retMap.put("holidays", holidayMap.values());

        retMap.put("errCode", 0);
        return retMap;
    }

    @RequestMapping(value="appinvs_holiday.do/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> setInstitutionHoliday(@RequestBody Map<String, Object> params) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("errCode", -1);
        DtInvsInstitutionHolidayEntity entity = new DtInvsInstitutionHolidayEntity();
        if (null != params.get("id")) {
            entity.setId(Integer.valueOf(params.get("id").toString()));
        }
        entity.setDate(params.get("date").toString());
        LocalDate today = LocalDate.now();
        LocalDate date = LocalDate.parse(entity.getDate());
        if (date.isBefore(today)) {
            retMap.put("errMsg", "今天以前的日期不允许修改。");
            return retMap;
        }
        entity.setInstitutionId(params.get("institutionId").toString());
        entity.setIsHoliday(Integer.valueOf(params.get("isHoliday").toString()));
        holidayService.saveHoliday(entity);
        retMap.put("errCode", 0);
        return retMap;
    }

    @RequestMapping(value="appinvs_holiday_weekly.do/load", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getWeeklyHoliday(@RequestBody Map<String, Object> params) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        String institutionId = params.get("institutionId").toString();
        retMap.put("detail", holidayService.getInstitutionWeeklyRes(institutionId));
        retMap.put("errCode", 0);
        return retMap;
    }

    @RequestMapping(value="appinvs_holiday_weekly.do/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> setWeeklyHoliday(@RequestBody WeeklyParamModel model) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("errCode", -1);
        holidayService.saveWeeklyRes(model);
        retMap.put("errCode", 0);
        return retMap;
    }

    @RequestMapping(value="appinvs_holiday.do/check", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> checkHoliday(@RequestBody Map<String, Object> params) {
        Map<String, Object> retMap = new HashMap<String, Object>();
        retMap.put("errCode", -1);
        String date = params.get("date").toString();
        String institutionId = params.get("institutionId").toString();
        retMap.put("result", holidayService.checkHoliday(date, institutionId) ? 1 : 0);
        retMap.put("errCode", 0);
        return retMap;
    }
}
